package com.example.demo.config;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 测试拦截器,继承的类参考 Spring源码中LocaleChangeInterceptor
 * 和ThemeChangeInterceptor写法
 * @Author https://www.javastudy.cloud
 * @CreateTime 2019/11/8
 **/
public class TestInterceptor extends HandlerInterceptorAdapter {


    /**
     * 这个方法在HandlerMapping找到对应的处理对象,但还没有请求这个对象时处理
     * 也就是在Controller的方法之前执行
     *
     * @param request
     * @param response
     * @param handler  这个参数就是代码着Controller的方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("run in pre Handle");
        return true;
    }


    /**
     * 在Handler/controller的方法处理完逻辑,渲染视图之前执行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("run in post Handle");
    }


    /**
     * 在视图渲染完之后执行,常用于资源清理等等
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }


}
